﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;
using KPIS.GERP.GAMS.App.App_UI;
using Telerik.Web.UI;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_MAS_ACCOUNTCODE : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();
        M_ACM_MAS_Account m_acm_mas_account = new M_ACM_MAS_Account();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load");
               // Session["UserID"] = "555";
                RadTreeView1.DataSource = d_acm_mas_account.GetList(Convert.ToInt32(Session["VersionSeq"]));
                RadTreeView1.DataBind();
                lblpVersionNo.Text = Session["VersionSeq"].ToString();
            }
        }
        protected void RadTreeView1_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
        {
            lblpParentAccount.Text = "";
            UpdateUI1("Tree");
            string strAccountSeq = e.Node.Value;
            M_ACM_MAS_Account info = d_acm_mas_account.GetInfo_ByAccountSeq_Version(main.ConvertNullInt(strAccountSeq), main.ConvertNullInt(lblpVersionNo.Text));
            //M_ACM_MAS_Account infoName = d_acm_mas_account.GetInfo_ByAccountSeqSearchName(main.ConvertNullInt(strAccountSeq));
            //txtParentAccountSeq.Text = info.account_seq.ToString();
            //txtAccountCode.Text = info.account_code;
            //txtAccountName.Text = info.account_name;
            //txtAccountDesc.Text = info.account_desc;
            //lblpParentAccount.Text = infoName.account_name;
            hAccountSeq.Text = info.account_seq.ToString();
            hAccountCode.Text = info.account_code;
            hAccountName.Text = info.account_name;
            hAccountDescr.Text = info.account_desc;
            hUpperAccountSeq.Text = info.upper_account_seq.ToString();
            lblpParentAccount.Visible = true;
            //lblpParentAccount.Text = infoName.account_name;
            txtAccountCode.Text = info.account_code;
            txtAccountName.Text = info.account_name;
            txtAccountDesc.Text = info.account_desc;
            txtGFMISCode.Text = info.gfmis_code;
            txtOldAccountCode.Text = info.old_account_code;
            txtGroupingName.Text = info.grouping_name;
            hGroupingName.Text = info.grouping_name;
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            UpdateUI1("Insert");
            M_ACM_MAS_Account infoName = d_acm_mas_account.GetInfo_ByAccountSeqSearchName(main.ConvertNullInt(hAccountSeq.Text));
            lblpParentAccount.Text = infoName.account_name;
            txtAccountCode.Focus();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            UpdateUI1("Update");

            M_ACM_MAS_Account info = d_acm_mas_account.GetInfo_ByAccountSeq_Version(main.ConvertNullInt(hAccountSeq.Text), main.ConvertNullInt(lblpVersionNo.Text));
            M_ACM_MAS_Account infoName = d_acm_mas_account.GetInfo_ByAccountSeqSearchName(main.ConvertNullInt(hUpperAccountSeq.Text));
            lblpParentAccount.Text = infoName.account_name;
           // txtParentAccountSeq.Text = info.upper_account_seq.ToString();
            txtAccountCode.Text = info.account_code;
            txtAccountName.Text = info.account_name;
            txtAccountDesc.Text = info.account_desc;
            txtGFMISCode.Text = info.gfmis_code;
            txtOldAccountCode.Text = info.old_account_code;
            txtGroupingName.Text = info.grouping_name;
            txtGroupingNo.Text = info.grouping_no;
            txtAccountCode.Focus();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (RadTreeView1.SelectedNode.ParentNode == null)
            {
                //lblStatus.Text = "ไม่สามารถลบกลุ่มได้";
            }
            else
            {
                string strParentAccountSeq = RadTreeView1.SelectedNode.ParentNode.Value;
                UpdateUI1("Delete");
                //m_acm_mas_account.version_seq = lblpVersionNo.Text;
                //m_acm_mas_account.account_seq = Convert.ToInt32(hAccountSeq.Text);
                //m_acm_mas_account.upper_account_seq = main.ConvertNullInt(hUpperAccountSeq.Text);
                //m_acm_mas_account.account_code = txtAccountCode.Text;
                //m_acm_mas_account.account_name = txtAccountName.Text;
                //m_acm_mas_account.account_desc = txtAccountDesc.Text;
                //m_acm_mas_account.record_status = "D";
                //m_acm_mas_account.updated_by = Convert.ToInt32(main.strEmpID);
                //m_acm_mas_account.updated_when = DateTime.Now;
                d_acm_mas_account.Delete(hAccountSeq.Text, Session["EmpSeq"].ToString());
                RadTreeView1.DataSource = d_acm_mas_account.GetList(Convert.ToInt32(Session["VersionSeq"].ToString()));
                RadTreeView1.DataBind();
                RadTreeView1.FindNodeByValue(strParentAccountSeq).Selected = true;
                ClearObj();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateCheck() == true)
            {
                UpdateUI1("Save");
                DisplayImageWarning(false);
                if (hStatus.Text == "Insert")
                {
                    m_acm_mas_account.version_seq = lblpVersionNo.Text;
                    m_acm_mas_account.upper_account_seq = main.ConvertNullInt(hAccountSeq.Text);//Convert.ToInt32(txtParentAccountSeq.Text);
                    m_acm_mas_account.account_code = txtAccountCode.Text;
                    m_acm_mas_account.account_name = txtAccountName.Text;
                    m_acm_mas_account.account_desc = txtAccountDesc.Text;
                    m_acm_mas_account.gfmis_code = txtGFMISCode.Text;
                    m_acm_mas_account.old_account_code = txtOldAccountCode.Text;
                    m_acm_mas_account.record_status = "U";
                    m_acm_mas_account.created_by = main.ConvertNullInt(main.strEmpID);
                    m_acm_mas_account.created_when = DateTime.Now;
                    m_acm_mas_account.full_path = "";
                    m_acm_mas_account.grouping_name = txtGroupingName.Text;
                    m_acm_mas_account.grouping_no = txtGroupingNo.Text;
                    int account_seq = d_acm_mas_account.Insert(m_acm_mas_account);

                    m_acm_mas_account.account_seq = account_seq;
                    m_acm_mas_account.full_path = d_acm_mas_account.GetFullPath(account_seq.ToString(), "/" + account_seq.ToString() + "/");
                    d_acm_mas_account.Update(m_acm_mas_account);
                }
                else if (hStatus.Text == "Update")
                {
                    m_acm_mas_account.version_seq = lblpVersionNo.Text;
                    m_acm_mas_account.account_seq = Convert.ToInt32(hAccountSeq.Text);
                    m_acm_mas_account.upper_account_seq = main.ConvertNullInt(hUpperAccountSeq.Text);//Convert.ToInt32(txtParentAccountSeq.Text);
                    m_acm_mas_account.account_code = txtAccountCode.Text;
                    m_acm_mas_account.account_name = txtAccountName.Text;
                    m_acm_mas_account.account_desc = txtAccountDesc.Text;
                    m_acm_mas_account.gfmis_code = txtGFMISCode.Text;
                    m_acm_mas_account.old_account_code = txtOldAccountCode.Text;
                    m_acm_mas_account.record_status = "U";
                    m_acm_mas_account.updated_by = main.ConvertNullInt(main.strEmpID);
                    m_acm_mas_account.updated_when = DateTime.Now;
                    m_acm_mas_account.full_path = d_acm_mas_account.GetFullPath(hAccountSeq.Text, "/" + hAccountSeq.Text + "/");
                    m_acm_mas_account.grouping_name = txtGroupingName.Text;
                    m_acm_mas_account.grouping_no = txtGroupingNo.Text;
                    d_acm_mas_account.Update(m_acm_mas_account);
                }
                RadTreeView1.DataSource = d_acm_mas_account.GetList(Convert.ToInt32(Session["VersionSeq"].ToString()));
                RadTreeView1.DataBind();

                RadTreeNode node = RadTreeView1.FindNodeByValue(hAccountSeq.Text);
                node.Selected = true;
                node.Expanded = true;
                node.ExpandParentNodes();
            }
            else
            {
                EnableText(true);
                btnSave.Enabled = true;
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
        }
        protected void btnMainMenu_Click(object sender, EventArgs e)
        {
            Response.Redirect("ACM_MAS_Account_Version.aspx");
        }
        //============================ control events ==============================
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    main.StrSelectWord = "< เลือกรายการรหัสบัญชี >";
                    btnInsert.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    DisplayImageWarning(false);
                    EnableText(false);
                    break;
                case "Insert":
                    hStatus.Text = "Insert";
                    lblStatus.Text = "เพิ่มข้อมูลย่อย";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    btnDelete.Enabled = false;
                    ClearObj();
                    EnableText(true);
                    break;
                case "Update":
                    hStatus.Text = "Update";
                    lblStatus.Text = "แก้ไขข้อมูลจากที่เลือก";
                    btnInsert.Enabled = false;
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    btnDelete.Enabled = false;
                    EnableText(true);
                    break;
                case "Delete":
                    lblStatus.Text = "ลบข้อมูลจากที่เลือก";
                    btnInsert.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = true;
                    btnDelete.Enabled = false;
                    DisplayImageWarning(false);
                    EnableText(false);
                    break;
                case "Save":
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnInsert.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnDelete.Enabled = false;
                    DisplayImageWarning(false);
                    EnableText(false);
                    break;
                case "Cancel":
                    hStatus.Text = "Cancel";
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnInsert.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnDelete.Enabled = false;
                    DisplayImageWarning(false);
                    ClearObj();
                    lblpParentAccount.Text = "";
                    EnableText(false);
                    break;
                case "Tree":
                    lblStatus.Text = "ข้อมูลจากผังบัญชีทั้งหมด";
                    btnInsert.Enabled = true;
                    btnEdit.Enabled = true;
                    btnDelete.Enabled = true;
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    break;
            }
        }
        //============================ clear events ==============================
        protected void ClearObj()
        {
            txtAccountCode.Text = "";
            txtAccountName.Text = "";
            txtAccountDesc.Text = "";
        }
        protected void DisplayImageWarning(bool pFlag)
        {
            imgWarnAccountCode.Visible = pFlag;
            imgWarnAccountName.Visible = pFlag;
        }
        protected void EnableText(bool pFlag)
        {
            txtAccountCode.Enabled = pFlag;
            txtAccountName.Enabled = pFlag;
            txtAccountDesc.Enabled = pFlag;
            txtGFMISCode.Enabled = pFlag;
            txtOldAccountCode.Enabled = pFlag;
            txtGroupingName.Enabled = pFlag;
            txtGroupingNo.Enabled = pFlag;
        }
        //============================ clear events ==============================
        protected bool ValidateCheck()
        {
            bool bFlag = true;
            if (txtAccountCode.Text == "")
            {
                imgWarnAccountCode.Visible = true;
                lblStatus.Text = "กรุณากรอกรหัสบัญชีด้วย";
                txtAccountCode.Focus();
                bFlag = false;
            }
            if (txtAccountName.Text == "")
            {
                imgWarnAccountName.Visible = true;
                lblStatus.Text = "กรุณากรอกชื่อบัญชีด้วย";
                txtAccountName.Focus();
                bFlag = false;
            }
            if (txtAccountCode.Text.Length > 20)
            {
                imgWarnAccountCode.Visible = true;
                lblStatus.Text = "รหัสบัญชีเกิน 20 ตัวอักษร";
                txtAccountCode.Focus();
                bFlag = false;
            }
            if (txtAccountName.Text.Length > 70)
            {
                imgWarnAccountName.Visible = true;
                lblStatus.Text = "ชื่อบัญชีเกิน 70 ตัวอักษร";
                txtAccountName.Focus();
                bFlag = false;
            }
            return bFlag;
        }

        protected void cmdCalFullPath_Click(object sender, EventArgs e)
        {
            IList<M_ACM_MAS_Account> models = d_acm_mas_account.GetList(null);

            foreach (var model in models)
            {
                if (string.IsNullOrEmpty(model.full_path))
                {
                    model.full_path = d_acm_mas_account.GetFullPath(model.account_seq.ToString(), "/" + model.account_seq.ToString() + "/");
                    model.updated_by = main.ConvertNullInt(main.strEmpID);
                    model.updated_when = DateTime.Now;
                    d_acm_mas_account.Update(model);
                }
            }
        }
    }
}
